﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_OrgPersTree_Pers_Relation : System.Web.UI.Page
{
    DeptPersManage MyDPM = new DeptPersManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            ViewState["VOrgType"] = Request.QueryString["OrgType"].ToString();   //组织类型
        }
        catch
        {
        }
        try
        {
            ViewState["VDeptID"] = Request.QueryString["DeptID"].ToString();   //组织ID
        }
        catch
        {
        }

        if (ViewState["VName"] == null || ViewState["VName"].ToString().CompareTo("") == 0)
        {
            ViewState["VName"] = "";
        }
        if (ViewState["VSex"] == null || ViewState["VSex"].ToString().CompareTo("") == 0)
        {
            ViewState["VSex"] = "";
        }
        if (ViewState["VPersCode"] == null || ViewState["VPersCode"].ToString().CompareTo("") == 0)
        {
            ViewState["VPersCode"] = "";
        }
        if (ViewState["VCardID"] == null || ViewState["VCardID"].ToString().CompareTo("") == 0)
        {
            ViewState["VCardID"] = "";
        }

        if (ViewState["VOrgID"] == null || ViewState["VOrgID"].ToString().CompareTo("") == 0)
        {
            ViewState["VOrgID"] = "";
        }

        if (ViewState["OpType"] == null)
        {
            ViewState["OpType"] = "1";
        }

        if (ViewState["SortExpression"] == null || ViewState["SortExpression"].ToString().CompareTo("") == 0)
        {
            ViewState["SortExpression"] = "a.ID";
        }

        if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
        {
            ViewState["SortDirection"] = " ";
        }
        //Bind("");
    }

    private void Bind(string sqlsort)
    {
        if (ViewState["OpType"].ToString() == "1")
        {
            GridView1.DataSource = MyDPM .GetPersInfo (ViewState["VName"].ToString ().Trim (), ViewState["VSex"].ToString ().Trim(), ViewState["VPersCode"].ToString ().Trim (), ViewState["VCardID"].ToString ().Trim (),ViewState["VOrgID"].ToString ().Trim (),sqlsort);
            GridView1.DataBind();
        }
    }
    protected void SearchCheckInInfo_ServerClick(object sender, EventArgs e)
    {
        ViewState["VName"] = Name.Text.ToString().Trim();
        ViewState["VSex"] = Sex.Value.ToString().Trim();
        ViewState["VPersCode"] = PersCode.Text.ToString().Trim();
        ViewState["VCardID"] = CardID.Text.ToString().Trim();
        ViewState["VOrgID"] = HChoiceID_1.Value.ToString().Trim();

        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];

        Bind(sSqlSort);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sql = "";
        if (ViewState["SortDirection"].ToString() == "desc")
        {
            ViewState["SortDirection"] = " ";
        }
        else
        {
            ViewState["SortDirection"] = "desc";
        }

        ViewState["SortExpression"] = e.SortExpression;

        sql = " order by  " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sql);
    }
    protected void ChoicePersID_ServerClick(object sender, EventArgs e)
    {
        try
        {
            if (ViewState["VDeptID"].ToString().Trim() == "" || ViewState["VDeptID"].ToString().Trim() == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请重新引用!');</script>");
                return;
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请重新引用!');</script>");
            return;
        }
       string sChoiceInfo = ChoiceInfo.Value.ToString().Trim();
       if (sChoiceInfo.ToString().Trim() != "")
       {
           string sRepeatPersName = "";
           string[] aPersInfo = sChoiceInfo.Split('|');
           int iPersInfo = aPersInfo.Length - 1;
           for (int i = 0; i < iPersInfo; i++)
           {
               string sBackPersName = MyDPM .JudgePersIDInOrgID (ViewState["VDeptID"].ToString().Trim(),aPersInfo [i]);
               if(sBackPersName != "")
               {
                   if(sRepeatPersName != "")
                   {
                       sRepeatPersName = sRepeatPersName + ",";
                   }
                   sRepeatPersName = sRepeatPersName + sBackPersName;
               }
               else
               {
                   MyDPM.RelationDeptPersInfo(ViewState["VDeptID"].ToString().Trim(), aPersInfo[i]);
               }
           }

           if (sRepeatPersName != "")
           {
               sRepeatPersName = "因为当前部门下面已存在\\n或者有删除标记的人员[" + sRepeatPersName + "]";
               ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('部分引用成功!\\n" + sRepeatPersName + "');window.close();</script>");
           }
           else
           {
               ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('引用成功!');window.close();</script>");
           }
           /*int iback = 0;
           iback = MyUM.RelationDeptPersInfo(ViewState["VDeptID"].ToString().Trim(), sChoiceInfo);
           if (iback == 1)
           {
               ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.close();</script>");
           }
           else
           {
               ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('选择操作失败!');</script>");
           }*/
       }
       else
       {
           ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择人员!');</script>");
       }

    }
}
